#include "ffmpegheader.h"

char *getAVError(int errNum)
{
    static char msg[32] = {0};
    av_strerror(errNum, msg, 32);
    return msg;
}

int64_t getRealTimeByPTS(int64_t pts, AVRational timebase)
{
    //pts转换为对应us值
    AVRational timebase_q = {1, AV_TIME_BASE};
    int64_t ptsTime = av_rescale_q(pts, timebase, timebase_q);
    return ptsTime;
}

void calcAndDelay(int64_t startTime, int64_t pts, AVRational timebase)
{
    int64_t ptsTime = getRealTimeByPTS(pts, timebase);

    //计算startTime到此刻的时间差值
    int64_t nowTime = av_gettime() - startTime;
    int64_t offset = ptsTime - nowTime;

    //大于2秒一般时间戳存在问题 延时无法挽救
    if(offset > 1000 && offset < 2*1000*1000)
        av_usleep(offset);
}

